package com.example.test10mvc.controller;

import com.mysql.cj.jdbc.result.ResultSetImpl;
import com.mysql.cj.result.Field;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: fxy
 * @Date: 2022/08/22/16:11
 * @Description:
 */
@RestController
public class ProcessTestController {
    @GetMapping("/processTest")
    public Map processTest() throws Exception{
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/springdb?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true";
        String user = "root";
        String password = "123456";
        Connection connection = DriverManager.getConnection(url,user,password);
        String sql = "select * from good";
        PreparedStatement statement = connection.prepareStatement(sql);
        ResultSetImpl resultSet = (ResultSetImpl) statement.executeQuery();
        Field[] fields = resultSet.getColumnDefinition().getFields();

        List<Map<String,Object>> dataList = new ArrayList<>();
        while(resultSet.next()){
            Map<String,Object> data = new HashMap<>();
            for(Field field:fields){
                data.put(field.getColumnLabel(),resultSet.getObject(field.getColumnLabel()));
            }
            dataList.add(data);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("data",dataList);
        return map;
    }
}
